package com.open.launcher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.open.launcher.R;
import com.open.launcher.base.MyApplication;
import com.open.launcher.presenter.HomePagePresenter;
import com.open.launcher.utils.APPLog;
import com.open.launcher.utils.Consts;
import com.open.launcher.utils.Paths;
import com.open.library.utils.PreferencesUtils;


/**
 * Created by wendal on 2017/9/23.
 */

public class HomePageBottomAdapter extends RecyclerView.Adapter<HomePageBottomAdapter.MyViewHolder> {
    private HomePagePresenter presenter;
    private int count;
    private RecyclerView parentRecycle;
    private Context context;
    private View focusview;
    private View currentView;
    public HomePageBottomAdapter(HomePagePresenter presenter, Context context, View focusview) {
        this.presenter = presenter;
        this.context = context;
        this.focusview = focusview;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_bottom_item, null);
        MyViewHolder viewHolder = new MyViewHolder(view);
        if (parentRecycle == null) {
            parentRecycle = (RecyclerView) parent;
            //parentRecycle.addOnScrollListener(new MovieWorldScrollListener());
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setPosition(position);
        presenter.onBindBottomData(holder, position);
        APPLog.printInfo(count + "onBindViewHolder" + position);
    }

    @Override
    public int getItemCount() {
        return count+1;
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements HomePagePresenter.BottomViewHolder {
        private int position;
        private ImageView image_item;
        private TextView bottom_text;
        private RelativeLayout bottom_layout;
        public MyViewHolder(View itemView) {
            super(itemView);
            image_item = (ImageView) itemView.findViewById(R.id.image_item);
            bottom_text = (TextView) itemView.findViewById(R.id.bottom_text);
            bottom_layout = (RelativeLayout) itemView.findViewById(R.id.bottom_layout);
            itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (b) {

                        view.bringToFront();
                        view.animate().scaleX(1.10f);
                        view.animate().scaleY(1.10f);
                        bottom_layout.setBackgroundResource(R.mipmap.bg_pic_s);
                    } else {
                        view.animate().scaleX(1.0f);
                        view.animate().scaleY(1.0f);
                        bottom_layout.setBackgroundResource(0);
                    }
                }
            });
            itemView.setOnClickListener(new SetOnClick());
        }

        public void setPosition(int position) {
            this.position = position;
        }

        @Override
        public void setImage(String imageUrl) {
            APPLog.printInfo(count + "count==>:" + position);
            if(imageUrl.equals("set")){
                image_item.setImageResource(R.mipmap.setting_menu);
            }else {
                Glide.with(context).load("http://"+ PreferencesUtils.getString(context, Consts.IP_ADDR_KEY) + imageUrl).into(image_item);
            }
        }

        @Override
        public void setText(String text) {
            bottom_text.setTypeface(MyApplication.face);
            bottom_text.setText(text);
        }

        class SetOnClick implements View.OnClickListener {

            @Override
            public void onClick(View v) {
                presenter.getBottomParams(position);
            }
        }
    }
}
